﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Metro.Core;

namespace Clock
{
    /// <summary>
    /// Interaction logic for Options.xaml
    /// </summary>
    public partial class Options : Window
    {
        public Options()
        {
            InitializeComponent();
        }

        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            IconImage.Source = new BitmapImage(new Uri(E.Path + "\\Clock\\Resources\\icon.png"));

            SettingsTitle.Text = Widget.Instance.LocaleManager.GetString("Settings");
            GeneralPageHeader.Text = Widget.Instance.LocaleManager.GetString("General");
            AboutPageHeader.Text = Widget.Instance.LocaleManager.GetString("About");
            TimeFormatTitle.Text = Widget.Instance.LocaleManager.GetString("TimeFormat");
            Mode1RadioButton.Content = Widget.Instance.LocaleManager.GetString("24h");
            Mode2RadioButton.Content = Widget.Instance.LocaleManager.GetString("12h");
            WidgetName.Text = Widget.Instance.LocaleManager.GetString("WidgetName");
            WidgetDescription.Text = Widget.Instance.LocaleManager.GetString("WidgetDescription");
            WidgetVersion.Text = Widget.Instance.LocaleManager.GetString("WidgetVersion") + " " + Assembly.GetExecutingAssembly().GetName().Version.ToString();
            OkButton.Content = Widget.Instance.LocaleManager.GetString("OK");
            CancelButton.Content = Widget.Instance.LocaleManager.GetString("Cancel");
            ApplyButton.Content = Widget.Instance.LocaleManager.GetString("Apply");

            if (Widget.Instance.Sett.TimeMode == 1)
                Mode1RadioButton.IsChecked = true;
            else
                Mode2RadioButton.IsChecked = true;
        }

        private void OkButtonClick(object sender, RoutedEventArgs e)
        {
            ApplySettings();
            this.Close();
        }

        private void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ApplyButtonClick(object sender, RoutedEventArgs e)
        {
            ApplySettings();
            ApplyButton.IsEnabled = false;
        }

        private void GeneralPageHeaderMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var s = (Storyboard)Resources["SwapAnimLeft"];
            s.Begin();
            GeneralPage.Visibility = Visibility.Visible;
            GeneralPageHeader.Foreground = Brushes.White;
            AboutPageHeader.Foreground = Brushes.Gray;
        }

        private void AboutPageHeaderMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var s = (Storyboard)Resources["SwapAnimRight"];
            s.Begin();
            AboutPage.Visibility = Visibility.Visible;
            GeneralPageHeader.Foreground = Brushes.Gray;
            AboutPageHeader.Foreground = Brushes.White;
        }

        private void SwapAnimRightCompleted(object sender, EventArgs e)
        {
            GeneralPage.Visibility = Visibility.Collapsed;
        }

        private void SwapAnimLeftCompleted(object sender, EventArgs e)
        {
            AboutPage.Visibility = Visibility.Collapsed;
        }

        private void SettingsChanged(object sender, RoutedEventArgs e)
        {
            ApplyButton.IsEnabled = true;
        }

        private void ApplySettings()
        {
            Widget.Instance.Sett.TimeMode = (bool)Mode2RadioButton.IsChecked ? 0 : 1;

            Widget.Instance.UpdateSettings();
        }
    }
}
